const layer = layui.layer

getInfouser()
function getInfouser() {
  $.ajax({
    method: 'GET',
    url: '/my/userinfo',
    headers: {
      Authorization: localStorage.getItem('token') || '',
    },
    success: function (res) {
      // console.log(res)
      if (res.status !== 0) return layer.msg(res.message)

      layer.msg(res.message)
      Areuser(res.data)
    },
    complete: function (res) {
      console.log(res)
      if (
        res.responseJSON.status === 1 &&
        res.responseJSON.message == '身份认证失败！'
      ) {
        localStorage.removeItem('token')

        location.href = '/login.html'
      }
    },
  })
}

function Areuser(user) {
  let name = user.nickname || user.username
  //欢迎文本
  $('#welcome').html(`欢迎${name}`)

  if (user.user_pic !== null) {
    $('.layui-nav-img').attr('src', user.user_pic).show()
    $('.text-avatar').hide()
  } else {
    const first = name[0].toUpperCase()
    $('.text-avatar').html(first).show()
    $('.layui-nav-img').hide()
  }
}

//点击退出
$('#exit').on('click', function (e) {
  e.preventDefault()
  layer.confirm('确认退出?', { icon: 3, title: '提示' }, function (index) {
    //do something
    localStorage.removeItem('token')
    location.href = '/login.html'
    layer.close(index)
  })
})
